#!/bin/bash

vxnet_id=${1}
user_id=${2}
action=${3}

if [[ ${vxnet_id} == "" ]] || [[ ${user_id} == "" ]] || [[ ${action} == "" ]]
then
  echo "usage: ./load_vxnet.sh <vxnet_id> <user_id> <check/rescue>"
  exit 1
fi

exec_sql="/pitrix/bin/exec_sql.py"
shell_pwd=$(cd "$( dirname "$0" )" && pwd)
if [ ! -d "${shell_pwd}/cache" ];
then
  mkdir ${shell_pwd}/cache
fi

check_vxnet(){
  rm -f ${shell_pwd}/cache/${vxnet_id}_miss_ip.cache
  get_vm_sql="select i.host_machine,i.instance_id from nic as n,instance as i where n.vxnet_id='${vxnet_id}' and n.owner='${user_id}' and n.instance_id=i.instance_id and i.status='running';"
  ${exec_sql} -d zone -c "${get_vm_sql}" 2>/dev/null|grep 'i-' >${shell_pwd}/cache/${vxnet_id}_check.cache
  if [[ $(cat ${shell_pwd}/cache/${vxnet_id}_check.cache|wc -l) == 0 ]]
  then
    echo "${vxnet_id} has no instance."
    exit 0
  fi
  vm_zone=$(awk '{print $4}' ${shell_pwd}/cache/${vxnet_id}_check.cache)
  for vm_id in ${vm_zone}
  do
    echo "$(cat ${shell_pwd}/cache/${vxnet_id}_check.cache|wc -l) / $(awk '{print $4}' ${shell_pwd}/cache/${vxnet_id}_check.cache |grep -n ${vm_id})"
    hyper_id=$(grep ${vm_id} ${shell_pwd}/cache/${vxnet_id}_check.cache|awk '{print $2}')
    ssh -Tq ${hyper_id} "/usr/bin/safe-guest-sh ${vm_id} '/sbin/ip a'" &>/dev/null
    if [[ $? != 0 ]]
    then
      continue
    fi
    instance_ip=$(ssh -Tq ${hyper_id} "/usr/bin/safe-guest-sh ${vm_id} '/sbin/ip a'|grep -A 2 -E 'eth0:|eth0@'|grep -v 'inet6'|grep 'inet'|awk -F'[/ ]+' '{print $3}'")
    if [[ $instance_ip == "" ]]
    then
      echo ${vm_id} >>${shell_pwd}/cache/${vxnet_id}_miss_ip.cache
    fi
  done
  miss_ip_num=$(cat ${shell_pwd}/cache/${vxnet_id}_miss_ip.cache 2>/dev/null|wc -l)
  if [[ ${miss_ip_num} == 0 ]]
  then
    echo "${vxnet_id} is healthy."
  else
    echo "$(cat ${shell_pwd}/cache/${vxnet_id}_miss_ip.cache|wc -l) instances has no private ip!"
    cat ${shell_pwd}/cache/${vxnet_id}_miss_ip.cache
  fi
}

rescue_vxnet(){
  rescue_vm_zone=$(cat ${shell_pwd}/cache/${vxnet_id}_miss_ip.cache 2>/dev/null)
  if [[ ${rescue_vm_zone} == "" ]]
  then
    echo "${vxnet_id} has no instance need to rescue, please run check vxnet first."
    exit 0
  fi
  for rescue_vm_id in ${rescue_vm_zone}
  do
    echo "${rescue_vm_zone} / $(grep -n ${rescue_vm_id} ${shell_pwd}/cache/${vxnet_id}_miss_ip.cache)"
    rescue_hyper_id=$(grep ${vm_id} ${shell_pwd}/cache/${vxnet_id}_check.cache|awk '{print $2}')
    first_check=$(ssh -Tq ${rescue_hyper_id} "/usr/bin/safe-guest-sh ${rescue_vm_id} '/sbin/ip a'|grep -A 2 -E 'eth0:|eth0@'|grep 'inet'|awk -F'[/ ]+' '{print $3}'")
    if [[ ${first_check} != "" ]]
    then
      continue
    fi
    ssh -Tq ${rescue_hyper_id} "/usr/bin/safe-guest-sh ${rescue_vm_id} 'dhclient eth0'"
    sleep 10
    rescue_instance_ip=$(ssh -Tq ${rescue_hyper_id} "/usr/bin/safe-guest-sh ${rescue_vm_id} '/sbin/ip a'|grep -A 2 -E 'eth0:|eth0@'|grep 'inet'|awk -F'[/ ]+' '{print $3}'")
    if [[ ${rescue_instance_ip} == "" ]]
    then
      ssh -Tq ${rescue_hyper_id} "/usr/bin/safe-guest-sh ${rescue_vm_id} 'ifdown eth0'"
      ssh -Tq ${rescue_hyper_id} "/usr/bin/safe-guest-sh ${rescue_vm_id} 'ifup eth0'"
    fi
    last_check=$(ssh -Tq ${rescue_hyper_id} "/usr/bin/safe-guest-sh ${rescue_vm_id} '/sbin/ip a'|grep -A 2 -E 'eth0:|eth0@'|grep 'inet'|awk -F'[/ ]+' '{print $3}'")
    if [[ ${last_check} == "" ]]
    then
      echo "${rescue_vm_id} cannot auto rescue, need to fix yourself."
    else
      echo "${rescue_vm_id} auto rescue successful."
    fi
  done
}

if [[ ${action} == "check" ]]
then
  check_vxnet
elif [[ ${action} == "rescue" ]]
then
  rescue_vxnet
else
  echo "usage: ./load_vxnet.sh <vxnet_id> <user_id> <check/rescue>"
  exit 1
fi
